package com.sunriz.motordrive.exception;

import com.sunriz.motordrive.domain.DTO.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * ClassName: GlobalExceptionHandler
 * Package: com.sunriz.motordrive.exception
 * Description:
 *
 * @Author BuTang
 * @Create 2024/8/23 12:00
 * @Version 1.0
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    // 捕获数据库连接失败的异常
//    @ExceptionHandler(CannotGetJdbcConnectionException.class)
////    public ResponseEntity<String> handleDatabaseConnectionException(CannotGetJdbcConnectionException ex) {
//    public void handleDatabaseConnectionException(CannotGetJdbcConnectionException ex) {
//
//        log.info("Database connection failed. Please try again later.", HttpStatus.INTERNAL_SERVER_ERROR);
//        // 可以记录异常信息并返回友好的错误消息
////        return new ResponseEntity<>("Database connection failed. Please try again later.", HttpStatus.INTERNAL_SERVER_ERROR);
//    }
//
//    // 捕获 MyBatis 的持久化异常
//    @ExceptionHandler(PersistenceException.class)
//    public ResponseEntity<String> handlePersistenceException(PersistenceException ex) {
//        return new ResponseEntity<>("Database query failed. Please try again later.", HttpStatus.INTERNAL_SERVER_ERROR);
//    }
//
//    // 捕获 MySQL 通信异常
//    @ExceptionHandler(CommunicationsException.class)
//    public ResponseEntity<String> handleCommunicationsException(CommunicationsException ex) {
//        return new ResponseEntity<>("Database communication error. Please check your connection.", HttpStatus.INTERNAL_SERVER_ERROR);
//    }




    /**
     * 处理 MissingServletRequestParameterException
     * 返回 HTTP 状态码 400
     * @param ex MissingServletRequestParameterException
     * @return 错误信息
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResponseEntity<Result> handleMissingServletRequestParameter(MissingServletRequestParameterException ex) {
        String parameterName = ex.getParameterName();
        String errorMessage = "Required request parameter '" + parameterName + "' is missing.";
        // 使用 ResultVO 封装错误信息
        Result<String> result = Result.fail(errorMessage, "400");
        // 返回带有 HTTP 状态码的 ResponseEntity
        return ResponseEntity
                .status(HttpStatus.BAD_REQUEST)
                .body(result);
    }




    @ExceptionHandler(CrcValidationException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResponseEntity<Result<String>> handleCrcValidationException(CrcValidationException ex) {
        // 构建错误信息
        String errorMessage = "CRC校验失败: " + ex.getMessage();
        log.error("CRC校验失败: " + ex.getMessage());
        // 使用 ResultVO 封装错误信息
        Result<String> result = Result.fail(errorMessage, "400");
        // 返回带有 HTTP 状态码的 ResponseEntity
        return ResponseEntity
                .status(HttpStatus.BAD_REQUEST)
                .body(result);
    }

    @ExceptionHandler(MotorException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResponseEntity<Result<String>> handleMotorException(MotorException ex) {

        // 使用 ResultVO 封装错误信息
        Result<String> result = Result.fail(ex.getMessage(), "400");
        // 返回带有 HTTP 状态码的 ResponseEntity
        return ResponseEntity
                .status(HttpStatus.BAD_REQUEST)
                .body(result);
    }

    /**
     * 处理空指针异常
     * @param ex 空指针异常
     */
    @ExceptionHandler(NullPointerException.class)
    public void handleGlobalException(NullPointerException ex) {
        // 打印异常堆栈信息
        log.error(ex.getMessage());
    }

    /**
     * 处理空指针异常
     * @param ex 空指针异常
     */
    @ExceptionHandler(WriteMapException.class)
    public void handleWriteMapException(NullPointerException ex) {
        // 打印异常堆栈信息
        log.error("WriteMapException,BaseSerialPortImpl出现问题，505行");
    }
    /**
     * 处理其他异常
     * @param ex 异常
     * @return 错误信息
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Result> handleGlobalException(Exception ex) {
        // 打印异常堆栈信息
//        ex.printStackTrace();
        log.error(ex.getMessage());
        // 构建自定义的错误信息
        Result result = new Result();
        ex.printStackTrace();
        // 自定义错误码
        result.setCode("500");
        // 错误信息
        result.setMessage("服务器发生错误：" + ex.getMessage());

        // 返回包含错误信息的 ResponseEntity 对象，状态码为 500（Internal Server Error）
        return new ResponseEntity<>(result, HttpStatus.INTERNAL_SERVER_ERROR);
    }

}
